package org.cying.main.controller;

import cn.hutool.http.HttpStatus;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import lombok.extern.slf4j.Slf4j;
import org.cying.main.domain.SampleResult;
import org.cying.main.domain.Student;
import org.cying.main.domain.Unsign;
import org.cying.main.handler.GlobalFlowHandler;
import org.cying.main.handler.SignFlowHandler;
import org.cying.main.service.UnSignService;
import org.cying.main.util.RsaUtil;
import org.cying.main.util.SignUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 未签到映射接口控制类
 * @author Cying
 */
@Slf4j
@RefreshScope
@RestController
@RequestMapping(value = "/unsign")
public class UnSignController {

    @Resource
    private UnSignService service;

    @Value("${cying.private-key}")
    private String privateKey;

    @DeleteMapping(value = "/delete/record")
    public SampleResult<Boolean> deleteRecord(
            @RequestParam(value = "department", required = true)  String department,
            @RequestParam(value = "group",      required = false) String group
    ) {
        return service.delete(department, group)
                ? new SampleResult<>("/unsign/delete/record", HttpStatus.HTTP_OK, true, "删除成功")
                : new SampleResult<>("/unsign/delete/record", HttpStatus.HTTP_NOT_ACCEPTABLE, false, "删除失败");
    }

    /**
     * 查询当前时间段未签到成员
     * @return 查询结果
     */
    @GetMapping(value = "/get/duty")
    @SentinelResource(
        value = "get-duty",
        blockHandlerClass = GlobalFlowHandler.class,
        blockHandler = "flowHandler"
    )
    public SampleResult<Map<Long, Student>> getDuty() {
        return new SampleResult<>("/unsign/get-duty", HttpStatus.HTTP_OK, SignUtils.getDutyMap());
    }
    
    /**
     * 根据学号获取未签到信息
     * @param sid 学号
     * @param name 姓名
     * @param exp 日期表达式
     * @param dept 部门
     * @param group 组别
     * @return 查询结果
     */
    @GetMapping(value = "/get/record")
    @SentinelResource(
            value = "get-record",
            blockHandlerClass = SignFlowHandler.class,
            blockHandler = "getUnsignsHandler"
    )
    public SampleResult<List<Unsign>> getRecord(
        @RequestParam(value = "sid", required = false) String sid,
        @RequestParam(value = "name", required = false) String name,
        @RequestParam(value = "exp", required = true) String exp,
        @RequestParam(value = "dept", required = false) String dept,
        @RequestParam(value = "group", required = false) String group
    ) {
        sid = RsaUtil.dencrypt(sid, privateKey);
        name = RsaUtil.dencrypt(name, privateKey);
        exp = RsaUtil.dencrypt(exp, privateKey);
        Long id = sid == null ? null : Long.valueOf(sid);
        List<Unsign> res = service.getUnSigns(id, name, exp, dept, group);
        return res.size() == 0
                ? new SampleResult<>("/unsign/get/record", HttpStatus.HTTP_OK, null, "未查询到相关记录")
                : new SampleResult<>("/unsign/get/record", HttpStatus.HTTP_OK, res);
    }

    /**
     * 将未签到的同学统一存入数据库并清空值班表
     * @param info 时间段
     * @return 添加结果
     */
    @PostMapping(value = "/post/record")
    @SentinelResource(
            value = "post-record",
            blockHandlerClass = GlobalFlowHandler.class,
            blockHandler = "flowHandler_s"
    )
    public SampleResult<Object> postRecord(
        @RequestParam(value = "info", required = true) String info
    ) {
        for (Student stu: SignUtils.getDutyMap().values()) {
            service.insert(stu.getSid(), info);
        }
        return new SampleResult<>("/unsign/post/record", HttpStatus.HTTP_OK);
    }

}
